#include "stdafx.h"
#include "LoginDlg.h"
#include "ui_LoginDlg.h"
#include "UserInfoDlg.h"
#include "AppFunctions.h"
#include "FrameHelper.h"
#include <QCryptographicHash>
#include <QKeyEvent>

UserInfo LoginDlg::userInfo;

LoginDlg::LoginDlg(QWidget *parent)
	: QDialog(parent)
{
	ui = new Ui::LoginDlg();
	ui->setupUi(this);

	bLogin = false;

	pFrameHelper = new FrameHelper(this);
	pFrameHelper->addDragWidget(this);
	pFrameHelper->setCloseButton(ui->btnClose);
	pFrameHelper->setMinimizedButton(ui->btnMin);

	ui->tipInfo->setVisible(false);
	ui->editName->installEventFilter(this);
	//connect(ui->editPwd, SIGNAL(returnPressed()), this, SLOT(on_btnLogin_clicked()));

	if (gMapConfigs.contains("LOGIN/USER"))
	{
		ui->editName->setText(gMapConfigs.value("LOGIN/USER").toString());
	}

	if (gMapConfigs.contains("LOGIN/PWD"))
	{
		ui->editPwd->setText(gMapConfigs.value("LOGIN/PWD").toString());
		ui->checkPwd->setChecked(true);
	}

}

LoginDlg::~LoginDlg()
{
	delete pFrameHelper;
	delete ui;
}

bool LoginDlg::login()
{
	if (exec() == QDialog::Accepted)
	{
		this->close();
	}
	return bLogin;
}

UserInfo LoginDlg::getUserInfo()
{
	return userInfo;
}

void LoginDlg::on_btnLogin_clicked()
{
	//lihaoceshi
	//QString strMsg;
	//loadPicture("image:/user2.jpg", "D:/qq123.jpg", strMsg);
	//loadPicture("D:/qq12345.jpg", "D:/qq123.jpg", strMsg);
	//savePicture("D:/qq123.jpg", "D:/user3.jpg", strMsg);
	//return;
	//lihaoceshi

    const QString & name = ui->editName->text();
    const QString & pwd = ui->editPwd->text();
	QString error;
	bLogin = check(name,pwd,error);
	if (bLogin == false)
	{
		Hint(ui->tipInfo, HT_ERROR, error);
		return;
	}

	if (ui->checkPwd->isChecked())
	{
		gMapConfigs["LOGIN/USER"] = name;
		gMapConfigs["LOGIN/PWD"] = pwd;
		saveConfig(*getConfig());
	}
	else
	{
		gMapConfigs.remove("LOGIN/USER");
		gMapConfigs.remove("LOGIN/PWD");
		saveConfig(*getConfig());
	}

	accept();
}

void LoginDlg::keyPressEvent(QKeyEvent *e)
{
	if (e->key() == Qt::Key_Return)
	{
		on_btnLogin_clicked();
	}
	//QDialog::keyPressEvent(e);
}

//============================================================================================================================================
bool LoginDlg::check(const QString &strUserId, const QString &strPassword, QString &error)
{
	error.clear();
	QRecord dsRecord;
	QString sql = QString("SELECT USER_ID,PASSWORD,USER_NAME FROM PUB_USERS WHERE USER_ID = '%1'").arg(strUserId);
	bool bInit = dsRecord.select(sql);
	if (dsRecord.rowCount() != 1)
	{
		error = tr("Name or password is not right");
		return false;
	}
	userInfo.strUserPasswd = dsRecord.getCollectStr(0, "PASSWORD");
	userInfo.strUserID = dsRecord.getCollectStr(0, "USER_ID");
	userInfo.strUserName = dsRecord.getCollectStr(0, "USER_NAME");

	QString strMd5;
	QByteArray bb;
	bb = QCryptographicHash::hash(strPassword.toLocal8Bit(), QCryptographicHash::Md5);
	strMd5.append(bb.toHex());

	if (strMd5.compare(userInfo.strUserPasswd, Qt::CaseInsensitive) == 0)
	{
		return true;
	}
	error = tr("Name or password is not right");
	return false;
}


